package edu.put.sk.nrpp;
import com.savarese.rocksaw.net.*;

import edu.put.sk.nrpp.ospf.packet.OSPFPacket;
import edu.put.sk.nrpp.ospf.packet.OSPFPacketVersion2Type4;
import edu.put.sk.nrpp.ospf.packet.lsa.LSA;
import edu.put.sk.nrpp.ospf.packet.lsa.LSAType1;
import java.net.InetAddress;
import java.util.ArrayList;

/*

$Rev: 16 $:     Revision of last commit
$Author: marcinwolicki $:  Author of last commit
$Date: 2010-11-21 01:16:40 +0100 (Sun, 21 Nov 2010) $:    Date of last commit
$HeadURL: https://marcinwolicki@network-routing-protocols-presenter.googlecode.com/svn/trunk/nrpp/src/edu/put/sk/Network.java $:
$Id: Network.java 16 2010-11-21 00:16:40Z marcinwolicki $:

*/

public class NetworkOSPF  extends Network {
	public NetworkOSPF(String ipAddress) {
		super();
		setIpAddress(ipAddress);
	}
	
	public NetworkOSPF() {
		super();
	}

	private boolean listen = true;
	private String _ipAddress;
	
	
	/**
	 * @param ipAddress the ipAddress to set
	 */
	public void setIpAddress(String ipAddress) {
		this._ipAddress = ipAddress;
	}
	/**
	 * @return the ipAddress
	 */
	public String getIpAddress() {
		return _ipAddress;
	}
	
	public static ArrayList<OSPFPacketVersion2Type4>LSAData=new ArrayList<OSPFPacketVersion2Type4>();
	
	public static boolean sleep=false;
	
	@Override
	protected void protocolMachine(){
		try {
		     
			RawSocket r = new RawSocket();
			
			r.open(RawSocket.PF_INET, 89);
			try{
				if(System.getProperty("os.name").toLowerCase().indexOf( "win" ) >= 0)
					r.bind(InetAddress.getByName("192.168.2.1"));
				r.setMulticast("224.0.0.5", "192.168.2.1");
			}
			catch(Exception e){
				e.getStackTrace();
			}
			//System.out.println(InetAddress.getByName("ospf-all.mcast.net"));
			
			
			while (listen) {
				byte[] buff=new byte[81920];
				int size=r.read(buff);
				byte[] data=new byte[size+1];
				for(int i=0; i<=(size); i++)
					data[i]=buff[i];
				IPPacket packet=new IPPacket(data);
					OSPFPacket ospfPacket=OSPFPacket.getInstance(packet);
				if(ospfPacket instanceof OSPFPacketVersion2Type4){
					
					new Thread(new Runnable() {
						
						@Override
						public void  run() {
							if(NetworkOSPF.sleep)
								return;
							NetworkOSPF.sleep=true;
							try {
								System.out.println("Marcin wstawaj!");
								Thread.sleep(10000);
								System.out.println("Tomek wstawaj!");
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							ArrayList<OSPFPacketVersion2Type4>_LSAData=NetworkOSPF.LSAData;
							NetworkOSPF.LSAData=new ArrayList<OSPFPacketVersion2Type4>();
							NetworkOSPF.sleep=false;
							
							for(OSPFPacketVersion2Type4 ospf: _LSAData){
								Router.getInstance(ospf);
							}
							
							observerList.update(Router.Dictionary);
							
							
							
						}
					}).start();
					OSPFPacketVersion2Type4 ospfv2t4=(OSPFPacketVersion2Type4) ospfPacket;
					LSA[]t=ospfv2t4.getLSAData();
					NetworkOSPF.LSAData.add(ospfv2t4);
					
				}

			}
			System.out.println("end");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
